package com.emmy.walnut.view;

import android.arch.lifecycle.Observer;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.emmy.walnut.R;
import com.emmy.walnut.adapter.ActivityAdapter;
import com.emmy.walnut.adapter.TimingAdapter;
import com.emmy.walnut.constant.AppConstant;
import com.emmy.walnut.model.Record;
import com.emmy.walnut.model.TimingObject;
import com.emmy.walnut.presenter.ActivityPresenter;
import com.emmy.walnut.presenter.iface.ActivityPIface;
import com.emmy.walnut.ui.TipDialog;
import com.emmy.walnut.util.LiveDataBus;
import com.emmy.walnut.view.iface.ActivityFraIface;
import com.qmuiteam.qmui.widget.dialog.QMUITipDialog;


public class ActivityFragment extends Fragment implements ActivityFraIface {

    private RecyclerView mActivityList;
    private RecyclerView mTimingList;

    private ActivityAdapter mActivityAdapter;
    private TimingAdapter mTimingAdapter;

    private ActivityPIface activityPIface;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_activity, container, false);
        initView(view);
        return view;
    }

    private void initView(View view){
        activityPIface = new ActivityPresenter(this);

        mActivityList = view.findViewById(R.id.activityList);
        mTimingList = view.findViewById(R.id.timing);

        mActivityAdapter = new ActivityAdapter(getContext(), AppConstant.activities);
        mTimingAdapter = new TimingAdapter(getContext(), AppConstant.undoneRecords);

        mActivityList.setLayoutManager(new StaggeredGridLayoutManager(3,
                StaggeredGridLayoutManager.VERTICAL));
        mTimingList.setLayoutManager(new LinearLayoutManager(getContext(),
                LinearLayoutManager.VERTICAL, false));

        mActivityList.setAdapter(mActivityAdapter);
        mTimingList.setAdapter(mTimingAdapter);

        mActivityAdapter.setOnItemClickListener(new ActivityAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                //同一时候请专注一件事情
                if (AppConstant.undoneRecords.size()!=0){
                    final TipDialog tipDialog = new TipDialog.Builder(getContext()).create();
                    tipDialog.show();
                    //2s后对话框自动消失
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            tipDialog.dismiss();
                        }
                    },2000);
                    return;
                }
                Record record = new Record(AppConstant.username,
                        AppConstant.activities.get(position).getId(),
                        AppConstant.activities.get(position).getLabelName(),
                        System.currentTimeMillis(),
                        -1);
                activityPIface.startRecord(record);
                mTimingAdapter.notifyDataSetChanged();
            }
        });

        //接收来自Adapter的停止请求
        LiveDataBus.get().
                with("endTiming", Record.class).
                observe(this, new Observer<Record>() {
                    @Override
                    public void onChanged(@Nullable Record record) {
                        activityPIface.endRecord(record);
                    }
                });
    }

    @Override
    public void showErrors(String errorMessage) {
        Toast.makeText(getContext(), errorMessage, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void startRecords() {
        mTimingAdapter.notifyDataSetChanged();
    }
}
